package com.zoe.onelink.common.entity;

import com.zoe.onelink.common.enumeration.ExceptionCode;
import com.zoe.onelink.common.enumeration.ExceptionCodeEnum;
import com.zoe.onelink.common.model.IResult;
import lombok.Data;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * <p>标题: </p>
 * <p>描述: </p>
 * <p>版权: Copyright (c) 2021</p>
 * <p>公司: 智业软件股份有限公司</p>
 *
 * @version: 1.0
 * @author: ruansheng
 * @date: 2021-07-20
 */
@Data
@Accessors(chain = true)
public class ResultVO<T> implements IResult<T>, Serializable {

    private static final long serialVersionUID = 5584323399256496326L;

    /**
     * 成功编码
     */
    public static final ExceptionCodeEnum SUCCESS_CODE = ExceptionCodeEnum.SUCCESS;
    /**
     * 异常编码
     */
    public static final ExceptionCodeEnum ERROR_CODE = ExceptionCodeEnum.BIZ_ERROR;

    /**
     * 警告编码(兼容旧版本)
     */
    @Deprecated
    public static final ExceptionCodeEnum WARN_CODE = ExceptionCodeEnum.WARN;
    /**
     * 警告并操作编码(兼容旧版本)
     */
    @Deprecated
    public static final ExceptionCodeEnum WARN_OPERATOR_CODE = ExceptionCodeEnum.WARN_OPERATOR;

    /**
     * 响应编码
     */
    @SuppressWarnings("AlibabaPojoNoDefaultValue")
    private String code = SUCCESS_CODE.getCode();

    /**
     * 响应数据
     */
    private T data;

    /**
     * 响应信息
     */
    private String message;

    /**
     * 异常详细信息
     */
    private String error;

    /**
     * 链路ID
     */
    private String traceId;

    /**
     * 异常处理指南 (暂时隐藏,有需要再开放)
     */
    private String guide;


    public static <T> ResultVO<T> success() {
        return new ResultVO<>();
    }

    public static <T> ResultVO<T> success(T data) {
        return new ResultVO<T>().setData(data);
    }

    public static <T> ResultVO<T> success(String message, T data) {
        return new ResultVO<T>().setMessage(message).setData(data);
    }

    public static <T> ResultVO<T> failure(ExceptionCode exceptionCode, String message, String error) {
        return new ResultVO<T>().setCode(exceptionCode.getCode()).setMessage(message).setError(error);
    }

    public static <T> ResultVO<T> failure(String message, String error) {
        return failure(ExceptionCodeEnum.BIZ_ERROR, message, error);
    }

    public static <T> ResultVO<T> failure(String message) {
        return ResultVO.failure(message, null);
    }

}
